package com.lizanyi.teaFriend.controller;

import com.github.pagehelper.PageInfo;
import com.lizanyi.teaFriend.pojo.Goods;
import com.lizanyi.teaFriend.service.GoodsService;
import com.lizanyi.teaFriend.service.PageService;
import org.apache.poi.ss.formula.ptg.AreaNPtg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.ParameterResolutionDelegate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class GoodsContorller {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private PageService pageService;

    @RequestMapping("/to")
    public String main(){
        return "redirect:/to/main";
    }
        //    商品总信息
    @RequestMapping(value = "/to/main" ,method = RequestMethod.GET)
    public String goodslist(@RequestParam(value = "pageNum" ,required = false,defaultValue = "1")  Integer pageNum,
                            @RequestParam(value = "pageSize",required = false) Integer pageSize,
                            Model model){
        PageInfo pageInfo=pageService.findUserByPage(pageNum,pageSize);
        model.addAttribute("pageInfo",pageInfo);
        List<Goods> goods=pageInfo.getList();
        model.addAttribute("goods",goods);
        return "main";
    }

    //分页商品
    @RequestMapping(value = "/to/main/{pageNum}" ,method = RequestMethod.GET)
    public String goodslist1(@PathVariable(value = "pageNum" ,required = false)  Integer pageNum,
                            @PathVariable(value = "pageSize",required = false) Integer pageSize,
                            Model model){
        System.out.println(pageNum);
        PageInfo pageInfo=pageService.findUserByPage(pageNum,pageSize);
        model.addAttribute("pageInfo",pageInfo);
        List<Goods> goodsList=pageInfo.getList();
        model.addAttribute("goods",goodsList);
        return "main";
    }


    //根据id查询商品信息
    @RequestMapping("/to/main/id")
    public String goodlistbyid(@RequestParam(value = "gid",required = false) Integer gid, ModelMap modelMap){

        if(gid==null){
            modelMap.addAttribute("toerror","查询条件不能为空");
            PageInfo<Goods> pageInfo=pageService.findUserByPage(1,5);
            modelMap.addAttribute("pageInfo",pageInfo);
        }else {
            Goods goodsList = goodsService.getGoodsListBygid(gid);
            System.out.println(goodsList);
            modelMap.addAttribute("goods",goodsList);
            PageInfo<Goods> pageInfo=pageService.findUserByPage(1,5);
            modelMap.addAttribute("pageInfo",pageInfo);
        }
        return "main";
    }

//    更新商品1
    @RequestMapping("/to/main/update")
    public String updateGood(@RequestParam("gid") Integer gid,
                             @RequestParam(value = "gname",required = false) String gname,
                             @RequestParam(value = "price",required = false) Double price,
                             @RequestParam(value = "num",required = false) Integer num,
                             ModelMap modelMap){
        int update=goodsService.updateGoodById(gid,gname,price,num);
        modelMap.addAttribute("success","成功更新1条数据");
        return  "redirect:/to/main";
    }



    //删除商品信息
    @RequestMapping(value = "/main/{id}" ,method = RequestMethod.DELETE)
    public String deleteGood(@PathVariable("id") Integer id){
        goodsService.deleteGoodById(id);
        return "redirect:/to/main";
    }


    //添加商铺
    @RequestMapping(value = "/to/main/add",method = RequestMethod.POST)
    public String addGood(@RequestParam(value = "gname",required = false) String gname,
                          @RequestParam(value = "price",required = false) Double price,
                          @RequestParam(value = "num",required = false) Integer num,
                          ModelMap modelMap){
        goodsService.addGood(null,gname,price,num);
        modelMap.addAttribute("success","添加成功");
        return "add";
    }

    //添加页面
    @RequestMapping("/to/add")
    public String toadd(){
        return "add";
    }



}
